package com.enrollandpay.web;

import com.enrollandpay.base.BaseController;
import com.enrollandpay.model.Activity;
import com.enrollandpay.service.ActivityService;
import com.enrollandpay.utils.CommonUtil;
import com.enrollandpay.utils.DateUtil;
import com.enrollandpay.utils.ResultJson;
import com.enrollandpay.utils.SubmitCheck;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 94504 on 2017/6/23.
 */
@Controller
@RequestMapping("/activity")
public class ActivityController
{
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/loadActivity")
    public String loadActivity(Integer id, HttpServletRequest request)
    {
        request.setAttribute("thisActivity", activityService.selectByPrimaryKey(id));
        request.getSession().setAttribute("token", DateUtil.getNo(3));

        return "save/activity_save";
    }


    @RequestMapping("/selectActivity")
    @ResponseBody
    public List<Activity> selectActivity()
    {
        List<Activity> list = activityService.selectAll();
        //return activityService.selectAll();
        return list;
    }


    @RequestMapping("/gotoActivity")
    public String gotoActivityPage()
    {
        return "main/activity";
    }

    /**
     * 新增或更新
     *
     * @param request
     * @param activity
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultJson save(HttpServletRequest request, Activity activity)
    {
        ResultJson resultJson = new ResultJson();
        Boolean flag = SubmitCheck.isRepeatSubmit(request);
        if (!flag)
            activityService.save(activity);
        else
            resultJson.setStatus("0");

        return resultJson;
    }

    /**
     * 根据ids删除对应的数据
     * @param separatorChar
     *         分割符
     */
    @RequestMapping("/deleteByIds")
    @ResponseBody
    public ResultJson deleteByIds(HttpServletRequest request,String ids, Character separatorChar)
    {

        ResultJson resultJson = activityService.deleteByIds(ids, separatorChar);

        return resultJson;

    }


    @RequestMapping("/deleteActivity")
    @ResponseBody
    public ResultJson deleteActivity(Integer id)
    {
        if(id != null)
           // activityService.deleteByPrimaryKey(id);
            System.out.println();

        return new ResultJson();
    }

}
